package jp.co.iris_ltd.irisphotoeditor.util;

import jp.co.iris_ltd.irisphotoeditor.IrisPhotoEditorActivity;
import jp.co.iris_ltd.irisphotoeditor.R;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

public class IrisPhotoEditorDialogBuilder {

    public static AlertDialog.Builder makeSaveDialog(final IrisPhotoEditorActivity activity) {
        final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity);
        dialogBuilder.setMessage(activity.getResources().getString(R.string.dialog_title_save_as));
        final EditText text = new EditText(activity);
        dialogBuilder.setView(text);
        dialogBuilder.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                activity.save(text.getText().toString());
            }
        });
        dialogBuilder.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        return dialogBuilder;

    }

    public static AlertDialog.Builder makeConfirmNewDialog(final IrisPhotoEditorActivity activity) {
        final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity);
        dialogBuilder.setTitle(R.string.title_exit);
        dialogBuilder.setMessage(R.string.confirm_new);
        dialogBuilder.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                activity.finish();
            }
        });
        dialogBuilder.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        return dialogBuilder;
    }

    public static AlertDialog.Builder makeSaveFileForTweetDialog(final IrisPhotoEditorActivity activity) {
        final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity);
        dialogBuilder.setMessage(R.string.dialog_message_save_for_tweet);
        dialogBuilder.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                activity.showSaveDialog();
            }
        });
        dialogBuilder.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        return dialogBuilder;
    }

    public static AlertDialog.Builder makeTweetDialog(final IrisPhotoEditorActivity activity) {
        final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity);
        dialogBuilder.setMessage(R.string.dialog_title_tweet);
        final EditText text = new EditText(activity);
        dialogBuilder.setView(text);
        dialogBuilder.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                new AsyncTask<Void, Void, Boolean>() {
                    @Override
                    protected void onPreExecute() {
                        super.onPreExecute();
                        activity.showProgressDialog();
                    }

                    @Override
                    protected Boolean doInBackground(Void... params) {
                        return activity.getTwitterManager().tweet(text.getText().toString(), activity.getNewestImagePath(), false);
                    };

                    @Override
                    protected void onPostExecute(Boolean result) {
                        activity.dismissProgressDialog();
                        if (result) {
                            activity.showToastMessage(R.string.toast_tweet_success);
                        } else {
                            activity.showToastMessage(R.string.toast_tweet_failed);
                        }
                    };
                }.execute();
            }
        });
        dialogBuilder.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        return dialogBuilder;
    }

    public static AlertDialog.Builder makeTwitterProgress(final IrisPhotoEditorActivity activity) {
        final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity);
        View progress = activity.getLayoutInflater().inflate(R.layout.twitter_progress, null);
        dialogBuilder.setView(progress);
        return dialogBuilder;
    }

    public static AlertDialog.Builder makeEffectListDialog(final IrisPhotoEditorActivity activity) {
        final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity);
        final String[] items = activity.getResources().getStringArray(R.array.effect_list);
        dialogBuilder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                Bitmap bitmap = activity.getCanvasBitmap();
                switch (item) {
                case 0:
                    activity.getCanvasImageView().setImageBitmap(Effector.effectFisheye(bitmap));
                    break;
                case 1:
                    activity.getCanvasImageView().setImageBitmap(Effector.effectMosaic(bitmap));
                    break;
                case 2:
                    activity.getCanvasImageView().setImageBitmap(Effector.filterMonoChrome(bitmap));
                    break;
                case 3:
                    activity.getCanvasImageView().setImageBitmap(Effector.filterSepia(bitmap));
                    break;
                case 4:
                    activity.getCanvasImageView().setImageBitmap(Effector.filterPositiveNegativeReversal(bitmap));
                    break;
                default:
                    break;
                }
                activity.getCanvasImageView().restoreCanvas();
            }
        });
        return dialogBuilder;
    }

    public AlertDialog.Builder makeColorFilterDialog(final IrisPhotoEditorActivity activity) {
        final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity);
        final ViewGroup layout = (ViewGroup) activity.getLayoutInflater().inflate(R.layout.color_selector, null);
        final Bitmap thumb = activity.getCanvasBitmap().copy(Bitmap.Config.ARGB_8888, true);
        final ImageView thumbImage = (ImageView) layout.findViewById(R.id.imgThumbnail);
        final TextView txtAlphaValue = (TextView) layout.findViewById(R.id.txtAlphaValue);
        final SeekBar seekAlpha = (SeekBar) layout.findViewById(R.id.seekAplha);
        final TextView txtRedValue = (TextView) layout.findViewById(R.id.txtRedValue);
        final SeekBar seekRed = (SeekBar) layout.findViewById(R.id.seekRed);
        final TextView txtGreenValue = (TextView) layout.findViewById(R.id.txtGreenValue);
        final SeekBar seekGreen = (SeekBar) layout.findViewById(R.id.seekGreen);
        final TextView txtBlueValue = (TextView) layout.findViewById(R.id.txtBlueValue);
        final SeekBar seekBlue = (SeekBar) layout.findViewById(R.id.seekBlue);

        thumbImage.setImageBitmap(thumb);
        txtAlphaValue.setText("0");
        txtRedValue.setText("0");
        txtGreenValue.setText("0");
        txtBlueValue.setText("0");

        seekAlpha.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Bitmap thumb = activity.getCanvasBitmap().copy(Bitmap.Config.ARGB_8888, true);
                int alpha = seekAlpha.getProgress();
                int red = seekRed.getProgress();
                int green = seekGreen.getProgress();
                int blue = seekBlue.getProgress();
                Effector.ColorFilter cf = new Effector.ColorFilter(alpha, red, green, blue);
                thumbImage.setImageBitmap(Effector.filterColorFilter(thumb, cf));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                txtAlphaValue.setText(":" + progress);
            }
        });
        seekRed.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Bitmap thumb = activity.getCanvasBitmap().copy(Bitmap.Config.ARGB_8888, true);
                int alpha = seekAlpha.getProgress();
                int red = seekRed.getProgress();
                int green = seekGreen.getProgress();
                int blue = seekBlue.getProgress();
                Effector.ColorFilter cf = new Effector.ColorFilter(alpha, red, green, blue);
                thumbImage.setImageBitmap(Effector.filterColorFilter(thumb, cf));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                txtRedValue.setText(":" + progress);
            }
        });
        seekGreen.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Bitmap thumb = activity.getCanvasBitmap().copy(Bitmap.Config.ARGB_8888, true);
                int alpha = seekAlpha.getProgress();
                int red = seekRed.getProgress();
                int green = seekGreen.getProgress();
                int blue = seekBlue.getProgress();
                Effector.ColorFilter cf = new Effector.ColorFilter(alpha, red, green, blue);
                thumbImage.setImageBitmap(Effector.filterColorFilter(thumb, cf));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                txtGreenValue.setText(":" + progress);
            }
        });
        seekBlue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                Bitmap thumb = activity.getCanvasBitmap().copy(Bitmap.Config.ARGB_8888, true);
                int alpha = seekAlpha.getProgress();
                int red = seekRed.getProgress();
                int green = seekGreen.getProgress();
                int blue = seekBlue.getProgress();
                Effector.ColorFilter cf = new Effector.ColorFilter(alpha, red, green, blue);
                thumbImage.setImageBitmap(Effector.filterColorFilter(thumb, cf));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                txtBlueValue.setText(":" + progress);
            }
        });

        dialogBuilder.setView(layout);
        dialogBuilder.setPositiveButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        dialogBuilder.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                int alpha = seekAlpha.getProgress();
                int red = seekRed.getProgress();
                int green = seekGreen.getProgress();
                int blue = seekBlue.getProgress();
                Effector.ColorFilter cf = new Effector.ColorFilter(alpha, red, green, blue);
                activity.getCanvasImageView().setImageBitmap(Effector.filterColorFilter(activity.getCanvasBitmap(), cf));
            }
        });

        return dialogBuilder;
    }

}
